clear all
set more off, permanently
set type double
cd ""

* Log file
capture log close
global date "`c(current_date)'"
log using "log_files/figures_appendix_$date.txt", text replace

**************************************************************************
**************** FIGURE A2: Incidence of Recall Referenda *****************
**************************************************************************

use "base_main.dta", clear 

duplicates drop district_id year, force 
keep if year==2002| year==2006 |year==2010  
sort year district_id

foreach var of varlist kits revocatoria resultado {
	by year: egen sum_`var' = sum(`var') 
}

gen share_RecallElection = sum_revocatoria/sum_kits
gen share_Recalled = sum_resultado/sum_revocatoria
global recall "kits revocatoria share_RecallElection resultado share_Recalled"

label var kits "Districts with kits purchase"
label var revocatoria "Districts with recall election"
label var share_RecallElection "Recall election conditional on kits"
label var resultado "Recalled mayor"
label var share_Recalled "Recalled mayor conditional on election"

// PANEL A
graph bar (mean) kits if year>=2002 & year<=2010, over(year, sort(order)) title("Panel A: Districts with Recall Referendum Attempts") ///
					  ytitle("% of Districts")			   
graph export "figures/ReferendumAttempts.eps", replace

// PANEL B
graph bar (mean) revocatoria if year>=2002 & year<=2010 , over(year, sort(order)) title("Panel B: Districts with Recall Referendum") ///
					   ytitle("% of Districts") 
graph export "figures/Referendums.eps", replace
	
// PANEL C
graph bar (mean) share_RecallElection if year>=2002 & year<=2010 , over(year, sort(order)) title("Panel C: Mayors facing Referendum, conditional on attempt") ///
					   ytitle("% Districts") 
graph export "figures/ConditionalReferendum.eps", replace
	
// PANEL D
graph bar (mean) resultado if year>=2002 & year<=2010 , over(year, sort(order)) title("Panel D: Districts with Recalled Mayors") ///
					   ytitle("% of Districts") 
graph export "figures/RecalledMayors.eps", replace

// PANEL E
graph bar (mean) share_Recalled if year>=2002 & year<=2010 , over(year, sort(order)) title("Panel E: Recalled mayors, conditional on referendum") ///
					   ytitle("% Districts")  
graph export "figures/ConditionalRecalled.eps", replace

****************************
**** Program lpolygraph ****
****************************

capture program drop lpolygraph 							
program define lpolygraph
args y x yt xt 

preserve

capture drop dembin
g dembin = int(`x'*200)/200 
egen Ey = mean(`y'), by(dembin)

collapse `x' `y' if dembin~=., by(dembin)

lpoly `y' `x' if `x'<=.5 
lpoly `y' `x' if `x'>.5 


lpoly `y' `x' if `x'<=0.5,  gen(z0 s0) ci se(se0)
lpoly `y' `x' if `x'>0.5,  gen(z1 s1) ci se(se1)
forvalues v=0/1 {
	gen ul`v' = s`v' + 1.96*se`v' 
	gen ll`v' = s`v' - 1.96*se`v' 
}

tw (scatter `y' `x', msymbol(oh) sort) /*
	*/ (line ul0 ll0 s0 z0, lcolor(blue blue black) lpattern(dash dash solid))  /*
	*/ (line ul1 ll1 s1 z1, lcolor(blue blue black) lpattern(dash dash solid)),  legend(off)  /*
	*/ xline(0.5, lpattern(solid) lwidth(thin) lcolor(gs5)) /*
	*/ xtitle(`xt') ytitle(`yt')  
	
drop   ul0 ll0 s0 z0 ul1 ll1 s1 z1 dembin
restore
end	

**************************************************************************
******** FIGURES A6-A7 - Continuity Test: Incumbents Edu and Exp  ********
**************************************************************************

use "base_main.dta", clear 

gen	election_winner =0
replace election_winner=1 if ELEGIDO=="SI" & inlist(year,2006,2010,2014)
replace election_winner=1 if winner==1 & year==2002
keep if election_winner==1
	
sort district_id year election_winner

*- FIGURE A6 - Continuity Test: incumbents education
	lpolygraph yrs_edu voterecall "Years of Education (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsEdu_Incumbent_lpoly", replace
	graph export "figures/YrsEdu_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph Uni voterecall "Attended University (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Uni_Incumbent_lpoly", replace
	graph export "figures/Uni_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph Tec voterecall "Attended Technical Education (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Technical_Incumbent_lpoly", replace
	graph export "figures/Technical_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph Sec2 voterecall "Completed Secondary (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Secondary_Incumbent_lpoly", replace
	graph export "figures/Secondary_Incumbent_lpoly.eps", as(eps) preview(on) replace
	graph combine "figures/YrsEdu_Incumbent_lpoly" "figures/Uni_Incumbent_lpoly" "figures/Technical_Incumbent_lpoly" "figures/Secondary_Incumbent_lpoly"
	graph export "figures/Education_Incumbent_lpoly.eps", as(eps) preview(on) replace

*- FIGURE A7 - Continuity Test: incumbents experience
	lpolygraph yrs_elected voterecall "Years of Elected Office Experience (t-1)" "Recall Vote Share (t)"
	graph save "figures/YrsElected_Incumbent_lpoly", replace
	graph export "figures/YrsElected_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph yrs_mayor voterecall "Years of Experience as Mayor (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsMayor_Incumbent_lpoly", replace
	graph export "figures/YrsMayor_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph yrs_partyoffice voterecall "Years of Party Experience (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsParty_Incumbent_lpoly", replace
	graph export "figures/YrsParty_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph work_public  voterecall "Public Sector Experience(t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/PublicSector_Incumbent_lpoly", replace
	graph export "figures/PublicSector_Incumbent_lpoly.eps", as(eps) preview(on) replace
	lpolygraph work_private voterecall "Private Sector Experience (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/PrivateSector_Incumbent_lpoly", replace
	graph export "figures/PrivateSector_Incumbent_lpoly.eps", as(eps) preview(on) replace
	graph combine "figures/YrsElected_Incumbent_lpoly" "figures/YrsMayor_Incumbent_lpoly" "figures/YrsParty_Incumbent_lpoly" "figures/PublicSector_Incumbent_lpoly" "figures/PrivateSector_Incumbent_lpoly"	 		
	graph export "figures/Experience_Incumbent_lpoly.eps", as(eps) preview(on) replace


**************************************************************************
******** FIGURE A8 - Continuity Test: Political variables  ****************
**************************************************************************

use "base_main.dta", clear 

sort district_id year 				
duplicates drop district_id year, force		

*- FIGURE A8 - Continuity Test: Political variables
	lpolygraph turnout voterecall "Election Turnout (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/Turnout_lpoly", replace
	graph export "figures/Turnout_lpoly.eps", as(eps) preview(on) replace
	lpolygraph candidates_NR voterecall "No. Candidates (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/NoCandidates_lpoly", replace
	graph export "figures/NoCandidates_lpoly.eps", as(eps) preview(on) replace
	lpolygraph win_margin voterecall "Win Margin (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/Margin_lpoly",  replace
	graph export "figures/Margin_lpoly.eps", as(eps) preview(on) replace
	lpolygraph PC voterecall "Political Competition (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/PolComp_lpoly", replace
	graph export "figures/PolComp_lpoly.eps", as(eps) preview(on) replace
	graph combine "figures/Turnout_lpoly" "figures/NoCandidates_lpoly" "figures/Margin_lpoly" "figures/PolComp_lpoly"
	graph export "figures/Political_lpoly.eps", as(eps) preview(on) replace



**************************************************************************
******** FIGURES A9-A10 - Continuity Test: Runners Up Education  ************
**************************************************************************

use "base_main.dta", clear 

*- FIGURE A9 - Continuity Test: Runners Up, Education
	lpolygraph runnersup_yrs_edu voterecall "Years of Education (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsEdu_RunnersUp_lpoly", replace
	graph export "figures/YrsEdu_RunnersUp_lpoly.eps", as(eps)  replace
	lpolygraph runnersup_Uni voterecall " Attended University (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Uni_RunnersUp_lpoly", replace
	graph export "figures/Uni_RunnersUp_lpoly.eps", as(eps) replace
	lpolygraph runnersup_Tec voterecall "Attended Technical Education (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Technical_RunnersUp_lpoly", replace
	graph export "figures/Technical_RunnersUp_lpoly.eps", as(eps)  replace
	lpolygraph runnersup_Sec2 voterecall "Completed Secondary (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/Secondary_RunnersUp_lpoly", replace
	graph export "figures/Secondary_RunnersUp_lpoly.eps", as(eps)  replace
	graph combine "figures/YrsEdu_RunnersUp_lpoly" "figures/Uni_RunnersUp_lpoly" "figures/Technical_RunnersUp_lpoly" "figures/Secondary_RunnersUp_lpoly" 
	graph export "figures/Education_RunnersUp_lpoly.eps", as(eps) preview(on) replace

*- FIGURE A10 - Continuity Test: Runners Up, Experience
	lpolygraph runnersup_yrs_elected voterecall "Years of Elected Office Experience (t-1)" "Recall Vote Share (t-1)"
	graph save "figures/YrsElected_RunnersUp_lpoly", replace
	graph export "figures/YrsElected_RunnersUp_lpoly.eps", as(eps) replace
	lpolygraph runnersup_yrs_mayor voterecall "Years of Experience as Mayor (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsMayor_RunnersUp_lpoly", replace
	graph export "figures/YrsMayor_RunnersUp_lpoly.eps", as(eps) replace
	lpolygraph runnersup_yrs_partyoffice voterecall "Years of Party Experience (t-1)" "Recall Vote Share (t-1)" 
	graph save "figures/YrsParty_RunnersUp_lpoly", replace
	graph export "figures/YrsParty_RunnersUp_lpoly.eps", as(eps)  replace
	lpolygraph runnersup_work_public  voterecall "Public Sector Experience(t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/PublicSector_RunnersUp_lpoly", replace
	graph export "figures/PublicSector_RunnersUp_lpoly.eps", as(eps) replace
	lpolygraph runnersup_work_private voterecall "Private Sector Experience (t-1) (%)" "Recall Vote Share (t-1)" 
	graph save "figures/PrivateSector_RunnersUp_lpoly", replace
	graph export "figures/PrivateSector_RunnersUp_lpoly.eps", as(eps)  replace
	graph combine "figures/YrsElected_RunnersUp_lpoly" "figures/YrsMayor_RunnersUp_lpoly" "figures/YrsParty_RunnersUp_lpoly" "figures/PublicSector_RunnersUp_lpoly" "figures/PrivateSector_RunnersUp_lpoly"
	graph export "figures/Experience_RunnersUp_lpoly.eps", as(eps) preview(on) replace

**************************************************************************
**************** FIGURE 9: McCrary Density Test **************************
**************************************************************************

use "base_main.dta", clear 

DCdensity voterecall_lag, breakpoint(0.5) generate(Xj Yj r0 fhat se_fhat) 
graph export "figures/maccrary.eps", as(eps) preview(on) replace

log close
